#include "task.h"
#include "windows.h"
#include "time.h"
#include <stdio.h>

///////////////////////////////////////////////////////////////////////////////

// #define FILE_TRACE

#include "file_trace.h"

///////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
// enums
//------------------------------------------------------------------------------

enum
{
	TASK_BUFFER_LENGTH = 1024,
	TASK_TIME_LENGTH = 3
};

//------------------------------------------------------------------------------
// public functions
//------------------------------------------------------------------------------

bool
TASK_Schedule(const taskschedule_t* a_taskschedule)
{  
	char  cmdline[TASK_BUFFER_LENGTH] = {0};
	char  str_hour[TASK_TIME_LENGTH] = {0};
	char  str_min[TASK_TIME_LENGTH] = {0};
	time_t  rawtime;
	STARTUPINFO  si;
    PROCESS_INFORMATION  pi;
	const char* last_slash = NULL;
    struct  tm* timeinfo = NULL;
	bool  result = false;

	// where is the last slash in the ini path?
	last_slash = strrchr(a_taskschedule->ini_path, '\\');

	// set the start time and hour
    time(&rawtime);
	rawtime += atoi(a_taskschedule->period) * 60; // set the start time based on the "period" mins specified
    timeinfo = localtime(&rawtime);
	sprintf(str_hour, "%.2d", timeinfo->tm_hour); // write 2 digits
	sprintf(str_min, "%.2d", timeinfo->tm_min);   // write 2 digits!

	// set the "command line"
	strcpy(cmdline, "schtasks /create /sc once /tn ");     // create, run once and task name
	strcat(cmdline, a_taskschedule->name);
	strcat(cmdline, " /tr \"");                            // exe path
	strncat(cmdline, a_taskschedule->ini_path, last_slash - a_taskschedule->ini_path); // ignore the ini path's ini file name part!
	strcat(cmdline, "\\");
	strcat(cmdline, EXE_FILE_NAME);
	strcat(cmdline, " --inipath ");
	strcat(cmdline, a_taskschedule->ini_path);
	strcat(cmdline, "\" /st ");                            // start time
	strcat(cmdline, str_hour);
	strcat(cmdline, ":");
	strcat(cmdline, str_min);
	strcat(cmdline, " /f");                                // supress warnings

	F_PRINT(("cmdline:|%s|\n", cmdline));

	// reset
    ZeroMemory( &si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi));
	
	//setup the time
	if (!CreateProcess(NULL,     // no module name: use command line
                      cmdline,   // command line
                      NULL,      // process handle not inheritable
                      NULL,      // thread handle not inheritable
                      FALSE,     // set handle inheritance to FALSE
                      0,         // no creation flags
                      NULL,      // use parent's environment block
                      NULL,      // use parent's starting directory                         
				      &si,       // pointer to STARTUPINFO structure
                      &pi))      // pointer to PROCESS_INFORMATION structure
	{
		goto exit;
	}

    // wait until child process exits
    WaitForSingleObject(pi.hProcess, INFINITE);

    // close process and thread handles
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

	result = true;

exit:

	F_PRINT(("result:|%s|\n", result ? "true": "false"));

	return result;
}
